﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using HsWslRf.HscStoreRf;
using Newtonsoft.Json;
using System.IO;
using System.Reflection;

namespace SmartRfMng.appcode
{
    public class StoreRf
    {
        private string m_websvr = string.Empty;
        private string m_host = string.Empty;
        private int m_timeout = 30;

        public StoreRf()
        {
            //读配置文件
            m_websvr = ZT_INI.GetINI("system", "WebSvr", "HscStoreRf");
            m_host = ZT_INI.GetINI("system", "Host", "");
            string sTimeOut = ZT_INI.GetINI("system", "TimeOut", "30");
            try
            {
                m_timeout = int.Parse(sTimeOut);
            }
            catch(Exception)
            {
                m_timeout = 30;
            }
        }

        public string Host
        {
            get { return m_host; }
            set { m_host = value; }
        }


        public string WebSvr
        {
            get { return m_websvr; }
            set { m_websvr = value; } 
        }

        public int TimeOut
        {
            get { return m_timeout; }
            set { m_timeout = value; }
        }

        //获取服务器日期时间
        public string GetServerDate()
        {
            try
            {
                if (m_websvr == "" || m_host == "")
                {
                    return "";
                }     

                HscStoreRfService storerf = new HscStoreRfService();
                storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                return storerf.GetServerDatetime();
            }
            catch(Exception)
            {
                return "";
            }
        }
       
        //处理批发销售
        public int DealWslPfXs(WslPfXsHead pfxs, out string sRtnMsg)
        {
            try
            {
                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return -1;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                string BillData = JsonConvert.SerializeObject(pfxs);
                return storerf.DealWslPfXs(BillData, out sRtnMsg);
            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return -2;
            }
        }

        //用户登陆
        public bool UserLogin(string sUserCode, string sPassWord, out string sUserId, 
            out string sUserName, out string sUserType, out string sRtnMsg)
        {
            
            sUserId = "-1";
            sUserName = string.Empty;
            sUserType = string.Empty;
            sRtnMsg = string.Empty;

            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return  false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                bool bResult = storerf.UserLogin(sUserCode, sPassWord, out sUserId, out sUserName, out sUserType, out sRtnMsg);
                if (bResult)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }

        //商品信息
        public bool DealPluInfo(out string sRtnMsg)
        {
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
               // storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                byte[] buffer = storerf.DealPluInfo(out sRtnMsg);
                if (buffer.Length > 1)
                {
                    byte[] pluinfo = GZip.Decompress(buffer);
                    string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);  
                    string FileName = CurrentPath + "\\PluFile.xml";
                    FileStream fs = File.Open(FileName, FileMode.Create);//建立文件流
                    try
                    {
                        fs.Write(pluinfo, 0, pluinfo.Length);//把数组写到文件中
                    }
                    finally
                    {
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }
                
            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }


        //批发客户信息
        public bool DealEtpCustInfo(out string sRtnMsg)
        {
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                // storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                byte[] buffer = storerf.DealEtpCustInfo(out sRtnMsg);
                if (buffer.Length > 1)
                {
                    byte[] etpinfo = GZip.Decompress(buffer);
                    string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string FileName = CurrentPath + "\\EtpFile.xml";
                    FileStream fs = File.Open(FileName, FileMode.Create);//建立文件流
                    try
                    {
                        fs.Write(etpinfo, 0, etpinfo.Length);//把数组写到文件中
                    }
                    finally
                    {
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }


        //员工（业务员、驾驶员）信息
        public bool DealUsrEmpInfo(out string sRtnMsg)
        {
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                // storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                byte[] buffer = storerf.DealUsrEmpInfo(out sRtnMsg);
                if (buffer.Length > 1)
                {
                    byte[] empinfo = GZip.Decompress(buffer);
                    string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string FileName = CurrentPath + "\\EmpFile.xml";
                    FileStream fs = File.Open(FileName, FileMode.Create);//建立文件流
                    try
                    {
                        fs.Write(empinfo, 0, empinfo.Length);//把数组写到文件中
                    }
                    finally
                    {
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }

        //车辆信息
        public bool DealDspTruckInfo(out string sRtnMsg)
        {
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                // storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                byte[] buffer = storerf.DealDspTruckInfo(out sRtnMsg);
                if (buffer.Length > 1)
                {
                    byte[] dspinfo = GZip.Decompress(buffer);
                    string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string FileName = CurrentPath + "\\TruckFile.xml";
                    FileStream fs = File.Open(FileName, FileMode.Create);//建立文件流
                    try
                    {
                        fs.Write(dspinfo, 0, dspinfo.Length);//把数组写到文件中
                    }
                    finally
                    {
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }

        //查询批发相关信息
        public bool QryWslPfXsInfo(string EtpCode, string YwyCode, string BillType, out string sRtnMsg)
        {
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                // storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                byte[] buffer = storerf.QryWslPfXsInfo(EtpCode, YwyCode, BillType, out sRtnMsg);
                if (buffer.Length > 1)
                {
                    byte[] xsinfo = GZip.Decompress(buffer);
                    string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string FileName = CurrentPath + "\\PfData.xml";
                    FileStream fs = File.Open(FileName, FileMode.Create);//建立文件流
                    try
                    {
                        fs.Write(xsinfo, 0, xsinfo.Length);//把数组写到文件中
                    }
                    finally
                    {
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }

        //查询批发相关信息表体
        public bool QryWslPfXsBodyInfo(string BillNo, out string sRtnMsg)
        {
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                // storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                byte[] buffer = storerf.QryWslPfXsBodyInfo(BillNo, out sRtnMsg);
                if (buffer.Length > 1)
                {
                    byte[] xsinfo = GZip.Decompress(buffer);
                    string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string FileName = CurrentPath + "\\PfDataBody.xml";
                    FileStream fs = File.Open(FileName, FileMode.Create);//建立文件流
                    try
                    {
                        fs.Write(xsinfo, 0, xsinfo.Length);//把数组写到文件中
                    }
                    finally
                    {
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }


        //查询批发相关支付信息
        public bool QryWslPfXsPayInfo(string BillNo, out string sRtnMsg)
        {
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    sRtnMsg = "没有配置WebService";
                    return false;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                // storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                byte[] buffer = storerf.QryWslPfXsPayInfo(BillNo, out sRtnMsg);
                if (buffer.Length > 1)
                {
                    byte[] xsinfo = GZip.Decompress(buffer);
                    string CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    string FileName = CurrentPath + "\\PfDataPay.xml";
                    FileStream fs = File.Open(FileName, FileMode.Create);//建立文件流
                    try
                    {
                        fs.Write(xsinfo, 0, xsinfo.Length);//把数组写到文件中
                    }
                    finally
                    {
                        fs.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception e)
            {
                sRtnMsg = e.Message.ToString();
                return false;
            }
        }


        //获取单据号
        public int GetBillNo(string sYwType, out string sBillNo, out string message)
        {
            sBillNo = string.Empty;
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    message = "没有配置WebService";
                    return -10;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                int iRtnCode = storerf.GetBillNo(sYwType, out sBillNo, out message);
                return iRtnCode;

            }
            catch (Exception e)
            {
                message = e.Message.ToString();
                return -1;
            }
        }


        //获取商品库存
        public int GetKcCount(string sPluId, out float fKyCount, out string message)
        {
            fKyCount = 0;
            try
            {

                if (m_websvr == "" || m_host == "")
                {
                    message = "没有配置WebService";
                    return -10;
                }

                HscStoreRfService storerf = new HscStoreRfService();
                storerf.Timeout = m_timeout * 1000;
                storerf.Url = @"http://" + m_host + @"/" + m_websvr + @"/HscStoreRfService.asmx";
                //storerf.Url = @"http://192.168.1.101/HscStoreRfService/HscStoreRfService.asmx";
                int iRtnCode = storerf.GetKcCount(sPluId, out fKyCount, out message);
                return iRtnCode;

            }
            catch (Exception e)
            {
                message = e.Message.ToString();
                return -1;
            }
        }
    }
}
